package com.wgy.designmode.action.mediator;

import com.wgy.designmode.action.mediator.colleague.Colleague;
import com.wgy.designmode.action.mediator.colleague.User;
import com.wgy.designmode.action.mediator.mediator.ChatRoom;
import com.wgy.designmode.action.mediator.mediator.Mediator;

/**
 * 中介者模式
 * 示例：聊天室作为中介者，用户作为同事对象。用户可以发送消息给聊天室，聊天室再转发给所有用户。
 * 使用场景：当系统中多个类相互耦合，形成网状结构时。
 */
public class MediatorPattern {

    public static void main(String[] args) {
        // 创建中介者
        Mediator chatRoom = new ChatRoom();

        // 创建同事对象
        Colleague user1 = new User(chatRoom, "Alice");
        Colleague user2 = new User(chatRoom, "Bob");
        Colleague user3 = new User(chatRoom, "Charlie");

        // 将同事对象添加到中介者中
        chatRoom.addColleague(user1);
        chatRoom.addColleague(user2);
        chatRoom.addColleague(user3);

        // 发送消息
        user1.send("Hello everyone!");
        user2.send("Hi Alice!");
    }
}
